﻿#include <cstdlib> // For std::putenv
#include <cstring> // For std::strdup
#include <tchar.h>
#include <qwidget.h>
#include <QHBoxLayout>
#include <QIcon>
#include <QVBoxLayout>
#include "windows.h"
#include "QWindow.h"
#include <tlhelp32.h>
#include <QProcess>
#include <QThread>
#include "T_CreateNewReportWidget.h"

#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/frame/XDesktop.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/uno/XInterface.hpp>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <com/sun/star/uno/Exception.hpp>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/document/IndexedPropertyValues.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <cppuhelper/bootstrap.hxx>

#include <osl/mutex.hxx>
#include <rtl/ustring.hxx>
#include <iostream>
#include <sal/main.h>
#include <osl/file.hxx>
#include <osl/process.h>
#include <rtl/process.h>
#include <rtl/ustrbuf.hxx>

#include "MainWindow.h"
#include "CreateNewReport.h"
#include "GenerateReportBase.h"
#include "AutomaticallySaveReport.h"

using namespace std;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::bridge;
using namespace com::sun::star::frame;
using namespace com::sun::star::sheet;
using namespace com::sun::star::registry;
using namespace com::sun::star::text;
using namespace rtl;

using com::sun::star::beans::PropertyValue;

using com::sun::star::text::XTextDocument;
using com::sun::star::text::XText;
using com::sun::star::text::XTextCursor;

namespace GenerateReport
{
	CreateNewReport::CreateNewReport()
	{
	}
	//test1::~Test1()
	//{
	//}

	void CreateNewReport::actionSlot_CreateNewReport()
	{
		_aboutPage = new T_CreateNewReportWidget();

		_aboutPage->hide();
		_aboutPage->setFixedSize(400, 200);
		_aboutPage->moveToCenter();
		_aboutPage->show();
	}

	void CreateNewReport::accept()
	{
		CreateNewDocument();
	}

	void CreateNewReport::CreateNewDocument()
	{

        auto _windowData = GenerateReportBaseInstance->createBlankDocument();
        qint64 _windowHWND = _windowData.first; //窗口句柄
        qint64 _windowID = _windowData.second;  //窗口映射ID
        if (_windowHWND)
        {
            GenerateReportBaseInstance->getMainWindow()->libreOfficeProcess(GenerateReport::ReportRelatedSubWidgetType::CreateNewReportSubWidget, _windowData, GenerateReportBaseInstance->getCurrentReportName());
            GenerateReportBaseInstance->setDocWindowVisible(_windowID, true);
        }
        else
        {
            std::cerr << "Could not get the window HWND." << std::endl;
        }

#if 0
        // 获取文本对象
        css::uno::Reference<css::text::XText> xText = xTextDocument->getText();
		//用于测试数据输入
		// 创建文本游标
		css::uno::Reference<css::text::XTextCursor> xTextCursor = xText->createTextCursor();

		// 在文档开头插入数据
		xTextCursor->gotoStart(false);
		OUString ouString = OUString::fromUtf8("English---Hello, World! ;  num----123456   中文：这些是中文");
		//OUString ouString = OUString::createFromAscii("English---Hello, World! ;  num----123456   中文：这些是中文");
		xTextCursor->setString(ouString);

		// 在文档结尾插入数据
		xTextCursor->gotoEnd(false);
		xTextCursor->setString(OUString::fromUtf8("这是插入到文本文档结尾的内容。"));

		std::cout << "successfully insert" << std::endl;
#endif
		GenerateReport::AutomaticallySaveReport _autoSave;
		_autoSave.AutomaticallySaveReportExcute();
		
		//std::cout << "-----1111-------" << QThread::currentThreadId() << std::endl;
		//const char* targetProcessName = "soffice.bin";		
		//DWORD processId = GetProcessIdByName(targetProcessName);
		//auto _hwnd = findWindowByProcessId(processId);
		//GenerateReport::GenerateReportBase::getinstance()->getMainWindow()->libreOfficeProcess(GenerateReport::ReportRelatedSubWidgetType::CreateNewReportSubWidget, (WId)_hwnd, GenerateReportBaseInstance->getCurrentReportName());
	}
}
